package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.dao.IndustryMapInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.IndustryMapInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.IndustryMapInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.IndustryMapInfoBean;
import cc.rengu.igas.bomp.facade.request.IndustryMapInfoQueryRequest;
import cc.rengu.igas.bomp.facade.response.IndustryMapInfoQueryResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhouyi
 * @version 1.0
 * @date 2021/4/26
 * @date 2019/08/15
 */
public class IndustryMapInfoQueryService extends ManageService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        IndustryMapInfoQueryRequest industryMapInfoQueryRequest = new IndustryMapInfoQueryRequest();
        ConvertUtil.convertOutput(industryMapInfoQueryRequest);
        return industryMapInfoQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        IndustryMapInfoQueryRequest industryMapInfoQueryRequest = (IndustryMapInfoQueryRequest) request;
        IndustryMapInfoQueryResponse industryMapInfoQueryResponse = new IndustryMapInfoQueryResponse();
        BizResponse<IndustryMapInfoQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setResult(industryMapInfoQueryResponse);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        String destChannel = industryMapInfoQueryRequest.getDestChannel();
        String industryType = industryMapInfoQueryRequest.getIndustryType();
        String publicPrivateFlag = industryMapInfoQueryRequest.getPublicPrivateFlag();
        /** 必填项校验 */
        if (StringUtil.isEmptyOrNull(industryType) || StringUtil.isEmptyOrNull(publicPrivateFlag)
                || StringUtil.isEmptyOrNull(destChannel)) {
            rglog.error("业务映射关系查询服务处理失败，必填项参数为空！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        IndustryMapInfoMapper industryMapInfoMapper = new IndustryMapInfoMapperImpl();
        rglog.info("开始查询映射关系");
        IndustryMapInfo industryMapInfo = industryMapInfoMapper.selectIndustryMapInfoByIndustryTypeAndChannelType(industryType, destChannel, publicPrivateFlag);
        rglog.info("查询映射关系结束");
        if (null == industryMapInfo) {
            rglog.error(RespCodeEnum.QUERY_INDUSTRY_MAP_ERROR.getRespDesc());
            throw new BizException(RespCodeEnum.QUERY_INDUSTRY_MAP_ERROR.getRespCode(), RespCodeEnum.QUERY_INDUSTRY_MAP_ERROR.getRespDesc());
        }
        /* 将信息添加到响应的集合中*/
        rglog.info("开始添加行业映射关系");
        List<IndustryMapInfoBean> industryMapInfoBeanList = getIndustryMapInfoList(industryMapInfo);
        rglog.info("添加行业映射关系结束");
        if (0 == industryMapInfoBeanList.size()) {
            rglog.error(RespCodeEnum.QUERY_INDUSTRY_MAP_ERROR.getRespDesc());
            throw new BizException(RespCodeEnum.QUERY_INDUSTRY_MAP_ERROR.getRespCode(), RespCodeEnum.QUERY_INDUSTRY_MAP_ERROR.getRespDesc());
        }
        industryMapInfoQueryResponse.setIndustryMapInfoList(industryMapInfoBeanList);
        return bizResponse;
    }

    private List<IndustryMapInfoBean> getIndustryMapInfoList(IndustryMapInfo industryMapInfo) {
        IndustryMapInfoBean industryMapInfoBean = new IndustryMapInfoBean();
        BeanUtil.beanCopy(industryMapInfo, industryMapInfoBean);
        List<IndustryMapInfoBean> industryMapInfoBeanList = new ArrayList<>();
        industryMapInfoBeanList.add(industryMapInfoBean);
        return industryMapInfoBeanList;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        IndustryMapInfoQueryResponse industryMapInfoQueryResponse = (IndustryMapInfoQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(industryMapInfoQueryResponse);
    }
}
